package at.fhooe.mc.msports;

import at.fhooe.mc.msports.ui.ColorableLabel;
import at.fhooe.mc.msports.ui.CustomizedButton;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.container.FullScreen;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.rim.device.api.ui.text.TextFilter;

public class PersonalDataScreen extends FullScreen{

	private EditField age, height, weight;
	private ObjectChoiceField gender;
	private ObjectChoiceField zone;
	private CheckboxField gps;
	
	public PersonalDataScreen(){
		initialize();
	}
	
	private void initialize(){
		setBackground(BackgroundFactory.createSolidBackground(
				Color.BLACK));
		
		ColorableLabel lbl = new ColorableLabel("Personal Data", Color.LIGHTBLUE,
				USE_ALL_WIDTH|DrawStyle.HCENTER, Font.getDefault().derive(Font.PLAIN,20));
		lbl.setMargin(20, 0, 0, 0);
		add(lbl);
		
		//Height
		HorizontalFieldManager hfmHeight = new HorizontalFieldManager();
		
		ColorableLabel lblHeight = new ColorableLabel("Height [cm]", Color.WHITE,
				USE_ALL_HEIGHT|DrawStyle.VCENTER,
				Font.getDefault().derive(Font.PLAIN,15)){
			protected void layout(int width, int height) {
				
				super.layout(90, 30);
				setExtent(90, 30);
			}
		};
		
		lblHeight.setPadding(8,0,0,0);
		hfmHeight.add(lblHeight);
		
		height = new EditField(USE_ALL_WIDTH|Field.FIELD_HCENTER){
			protected void layout(int width, int height) {
			
				super.layout(80, 30);
				setExtent(80, 30);
			}
		};
		height.setPadding(6,0,0,5);
		height.setMaxSize(6);
		height.setFilter(TextFilter.get(TextFilter.REAL_NUMERIC));
		height.setBackground(BackgroundFactory.createSolidBackground(Color.LIGHTGRAY));
		
		hfmHeight.add(height);
		hfmHeight.setMargin(20,0,0,55);
		
		ColorableLabel lblHeightHint = new ColorableLabel("130 - 230", Color.WHITE,
				USE_ALL_HEIGHT|DrawStyle.HCENTER,
				Font.getDefault().derive(Font.PLAIN,15)){
			protected void layout(int width, int height) {
				
				super.layout(90, 30);
				setExtent(90, 30);
			}
		};
		
		lblHeightHint.setPadding(8,0,0,8);
		hfmHeight.add(lblHeightHint);
		add(hfmHeight);
		
		//Weight
		HorizontalFieldManager hfmWeight = new HorizontalFieldManager();
		
		ColorableLabel lblWeight = new ColorableLabel("Weight [kg]", Color.WHITE,
				USE_ALL_HEIGHT|DrawStyle.VCENTER,
				Font.getDefault().derive(Font.PLAIN,15)){
			protected void layout(int width, int height) {
				
				super.layout(90, 30);
				setExtent(90, 30);
			}
		};
		
		lblWeight.setPadding(8,0,0,0);
		hfmWeight.add(lblWeight);
		
		weight = new EditField(USE_ALL_WIDTH|Field.FIELD_HCENTER){
			protected void layout(int width, int height) {
			
				super.layout(80, 30);
				setExtent(80, 30);
			}
		};
		weight.setPadding(6,0,0,5);
		weight.setMaxSize(6);
		weight.setFilter(TextFilter.get(TextFilter.REAL_NUMERIC));
		weight.setBackground(BackgroundFactory.createSolidBackground(Color.LIGHTGRAY));
		
		hfmWeight.add(weight);
		hfmWeight.setMargin(10,0,0,55);
		
		ColorableLabel lblWeightHint = new ColorableLabel("40 - 180", Color.WHITE,
				USE_ALL_HEIGHT|DrawStyle.HCENTER,
				Font.getDefault().derive(Font.PLAIN,15)){
			protected void layout(int width, int height) {
				
				super.layout(90, 30);
				setExtent(90, 30);
			}
		};
		
		lblWeightHint.setPadding(8,0,0,8);
		hfmWeight.add(lblWeightHint);
		add(hfmWeight);
		
		//Age
		HorizontalFieldManager hfmAge = new HorizontalFieldManager();
		
		ColorableLabel lblAge = new ColorableLabel("Age [y]", Color.WHITE,
				USE_ALL_HEIGHT|DrawStyle.VCENTER,
				Font.getDefault().derive(Font.PLAIN,15)){
			protected void layout(int width, int height) {
				
				super.layout(90, 30);
				setExtent(90, 30);
			}
		};
		
		lblAge.setPadding(8,0,0,0);
		hfmAge.add(lblAge);
		
		age = new EditField(USE_ALL_WIDTH|Field.FIELD_HCENTER){
			protected void layout(int width, int height) {
			
				super.layout(80, 30);
				setExtent(80, 30);
			}
		};
		age.setPadding(6,0,0,5);
		age.setMaxSize(3);
		age.setFilter(TextFilter.get(TextFilter.INTEGER));
		age.setBackground(BackgroundFactory.createSolidBackground(Color.LIGHTGRAY));
		
		hfmAge.add(age);
		hfmAge.setMargin(10,0,0,55);
		
		ColorableLabel lblAgeHint = new ColorableLabel("10 - 110", Color.WHITE,
				USE_ALL_HEIGHT|DrawStyle.HCENTER,
				Font.getDefault().derive(Font.PLAIN,15)){
			protected void layout(int width, int height) {
				
				super.layout(90, 30);
				setExtent(90, 30);
			}
		};
		
		lblAgeHint.setPadding(8,0,0,8);
		hfmAge.add(lblAgeHint);
		add(hfmAge);
		
		//Gender
		HorizontalFieldManager hfmGender = new HorizontalFieldManager();
		
		gender = new ObjectChoiceField("Gender    ",new String[]{"Male","Female"},
				0,USE_ALL_WIDTH|Field.FIELD_HCENTER){
			protected void paint(Graphics graphics) {
				graphics.setColor(Color.WHITE);
				super.paint(graphics);
			};

			public Object getChoice(int index) throws IllegalArgumentException {
				if(index == 0)
					return new String("Male");
				else
					return new String("Female");
			}
		};
		gender.setPadding(6,0,0,0);
		gender.setFont(Font.getDefault().derive(Font.PLAIN,15));
		hfmGender.add(gender);
		hfmGender.setMargin(10,0,0,55);
		add(hfmGender);

		//Zone
		HorizontalFieldManager hfmZone = new HorizontalFieldManager();
		
		zone = new ObjectChoiceField("Zone       ",new String[]{"0 - No sports",
				"1 - Walk for pleasure",
				"2 - Up to 1h per week",
				"3 - Over 1h per week",
				"4 - Less than 1 mile per week",
				"5 - 1 to 5 miles per week",
				"6 - 5 to 10 miles per week",
				"7 - Over 10 miles per week"},
				0,USE_ALL_WIDTH|Field.FIELD_HCENTER){
			protected void paint(Graphics graphics) {
				graphics.setColor(Color.WHITE);
				super.paint(graphics);
			};

			public Object getChoice(int index) throws IllegalArgumentException {
				switch(index){
				case 0: return "0 - No sports";
				case 1: return "1 - Walk for pleasure";
				case 2: return "2 - Up to 1h per week";
				case 3: return "3 - Over 1h per week";
				case 4: return "4 - Less than 1 mile per week";
				case 5: return "5 - 1 to 5 miles per week";
				case 6: return "6 - 5 to 10 miles per week";
				default: return "7 - Over 10 miles per week";
				}
			}
		};
		zone.setPadding(6,0,0,0);
		zone.setFont(Font.getDefault().derive(Font.PLAIN,15));
		hfmZone.add(zone);
		hfmZone.setMargin(10,0,0,55);
		add(hfmZone);
		
		//GPS
		gps = new CheckboxField("   Use GPS?", true, USE_ALL_WIDTH|Field.FIELD_HCENTER){
			protected void paint(Graphics graphics) {
				graphics.setColor(Color.LIGHTGRAY);
				super.paint(graphics);
			}
			
			protected void layout(int width, int height) {
				
				super.layout(120, 30);
				setExtent(120, 30);
			}
		};
		gps.setFont(Font.getDefault().derive(Font.PLAIN,18));
		gps.setMargin(15,0,0,140);
		add(gps);
		
		HorizontalFieldManager hfmButtons = new HorizontalFieldManager();
		
		//Commit button
		CustomizedButton btnCommit = new CustomizedButton(USE_ALL_WIDTH|Field.FIELD_HCENTER){
    		protected void layout(int width, int height) {
    			super.layout(getPreferredWidth(), getPreferredHeight());
    			setExtent(getPreferredWidth(), getPreferredHeight());
    		};
    		
    		protected boolean navigationUnclick(int status, int time) {
    			onStateChanged();
    			return true;
    		};
    		
    		protected boolean trackwheelUnclick(int status, int time) {
    			onStateChanged();
    			return true;
    		};
    		
    		public void onStateChanged(){
    			if(validate()){
    				PersonalData.setAge(Integer.parseInt(age.getText()));
    				PersonalData.setHeight(Float.parseFloat(height.getText()));
    				PersonalData.setWeight(Float.parseFloat(weight.getText()));
    				PersonalData.setGPS(gps.getChecked());
    				PersonalData.setMale(gender.getSelectedIndex()==0);
    				PersonalData.setZone(zone.getSelectedIndex());
    				PersonalData.updateData();
    				popScreen();
    			}
    		}
		};
		
		btnCommit.setWidth(100);
		btnCommit.setHeight(30);
		btnCommit.setLabel("Save");
		btnCommit.setFont(Font.getDefault().derive(Font.PLAIN, 20));
		btnCommit.setMargin(20,0,0,30);
    	hfmButtons.add(btnCommit);
    	
    	CustomizedButton btnAbort = new CustomizedButton(USE_ALL_WIDTH|Field.FIELD_HCENTER){
    		protected void layout(int width, int height) {
    			super.layout(getPreferredWidth(), getPreferredHeight());
    			setExtent(getPreferredWidth(), getPreferredHeight());
    		};
    		
    		protected boolean navigationUnclick(int status, int time) {
    			onStateChanged();
    			return true;
    		};
    		
    		protected boolean trackwheelUnclick(int status, int time) {
    			onStateChanged();
    			return true;
    		};
    		
    		public void onStateChanged(){
    				popScreen();	
    		}
		};
		
		btnAbort.setWidth(100);
		btnAbort.setHeight(30);
		btnAbort.setLabel("Abort");
		btnAbort.setFont(Font.getDefault().derive(Font.PLAIN, 20));
		btnAbort.setMargin(20,0,0,20);
    	hfmButtons.add(btnAbort);
    	
    	add(hfmButtons);
    	
    	loadData();
	}
	
	public void loadData(){
		UiApplication.getUiApplication().invokeLater(new Runnable() {
			
			public void run() {
				height.setText(String.valueOf(PersonalData.getHeight()));
				weight.setText(String.valueOf(PersonalData.getWeight()));
				age.setText(String.valueOf(PersonalData.getAge()));
				gender.setSelectedIndex(PersonalData.isMale()?0:1);
				zone.setSelectedIndex(PersonalData.getZone());
				gps.setChecked(PersonalData.useGPS());
			}
		});
	}
	
	public boolean validate(){
		
		//If all are empty...
		if(height.getText().compareTo("") == 0 &&
				height.getText().compareTo("") == 0 &&
				height.getText().compareTo("") == 0)
		{
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					Dialog.alert("All fields must have a value!");			
				}
			});
			
			height.setFocus();
			return false;
		}
		
		//Validate height
		if(height.getText().compareTo("") != 0){
			int h = (int)Float.parseFloat(height.getText());
			if(h < PersonalData.MIN_HEIGHT || h > PersonalData.MAX_HEIGHT){

				UiApplication.getUiApplication().invokeLater(new Runnable() {
					public void run() {
						Dialog.alert("Height must be between " + PersonalData.MIN_HEIGHT + " cm and " +
								PersonalData.MAX_HEIGHT + " cm!");
						height.setText("");
						height.setFocus();
					}
				});
				
				return false;
			}
		}else{
			
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					Dialog.alert("All fields must have a value");
					height.setFocus();
					
				}
			});
			return false;
		}
		
		//Validate weight
		if(weight.getText().compareTo("") != 0){
			int h = (int)Float.parseFloat(weight.getText());
			if(h < PersonalData.MIN_WEIGHT || h > PersonalData.MAX_WEIGHT){

				UiApplication.getUiApplication().invokeLater(new Runnable() {
					public void run() {
						Dialog.alert("Weight must be between " + PersonalData.MIN_WEIGHT + " and " +
								PersonalData.MAX_WEIGHT + " kg!");
						weight.setText("");
						weight.setFocus();
					}
				});
				
				return false;
			}
		}else{
			
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					Dialog.alert("All fields must have a value");
					weight.setFocus();
					
				}
			});
			return false;
		}
		
		//Validate age
		if(age.getText().compareTo("") != 0){
			int h = Integer.parseInt(age.getText());
			if(h < PersonalData.MIN_AGE || h > PersonalData.MAX_AGE){

				UiApplication.getUiApplication().invokeLater(new Runnable() {
					public void run() {
						Dialog.alert("Age must be between " + PersonalData.MIN_AGE + " and " +
								PersonalData.MAX_AGE + " years!");
						age.setText("");
						age.setFocus();
					}
				});
				
				return false;
			}
		}else{
			
			UiApplication.getUiApplication().invokeLater(new Runnable() {
				public void run() {
					Dialog.alert("All fields must have a value");
					age.setFocus();
					
				}
			});
			return false;
		}
		
		return true;
	}
	
	public void popScreen(){
		UiApplication.getUiApplication().popScreen(this);
	}
}
